package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApLikesBehaviorMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Description:
 * @Version: V1.0
 */
@Service
@Transactional
public class ApLikesBehaviorServiceImpl extends ServiceImpl<ApLikesBehaviorMapper, ApLikesBehavior>
        implements ApLikesBehaviorService {

    /**
     * 存储喜欢数据
     *
     * @param dto
     * @return
     */
    @Autowired
    ApBehaviorEntryService apLikesBehaviorService;

    @Override
    public ResponseResult like(LikesBehaviorDto dto) {


        //1.参数检验
        if (dto == null || dto.getArticleId() == null ||
                dto.getOperation().intValue() < 0 || dto.getOperation().intValue() > 1 ||
                dto.getType().intValue() < 0 || dto.getType().intValue() > 1) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //登录
        ApUser user = AppThreadLocalUtils.getUser();
        //判断用户是否登录
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN, "请先登录");
        }
        //2.查询行为实体 主键
        ApBehaviorEntry behaviorEntry = apLikesBehaviorService.findByUserIdOrEquipmentId(user.getId(), null);
        if (behaviorEntry == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //3.查询点赞行为是否存在
        ApLikesBehavior apLikesBehavior = getOne(Wrappers.<ApLikesBehavior>lambdaQuery()
                .eq(ApLikesBehavior::getArticleId, dto.getArticleId())
                .eq(ApLikesBehavior::getEntryId, behaviorEntry.getId()));

        //4.不存在，判断是否是点赞行为
        if (apLikesBehavior == null && dto.getOperation().equals(ApLikesBehavior.Operation.CANCEL.getCode())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "暂无点赞");
        }

        //5.已存在，判断重复点赞
        if (apLikesBehavior != null && dto.getOperation().intValue() == apLikesBehavior.getOperation().intValue() &&
                apLikesBehavior.getOperation().equals(ApLikesBehavior.Operation.LIKE.getCode()) &&
                ApLikesBehavior.Operation.LIKE.getCode() == dto.getOperation().shortValue()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "请勿重复点赞");
        }
        //保存点赞
        if (apLikesBehavior == null && dto.getOperation().equals(ApLikesBehavior.Operation.LIKE.getCode())) {
            apLikesBehavior = new ApLikesBehavior();
            apLikesBehavior.setArticleId(dto.getArticleId());
            apLikesBehavior.setCreatedTime(new Date());
            apLikesBehavior.setOperation(dto.getOperation());
            apLikesBehavior.setType(dto.getType());
            apLikesBehavior.setEntryId(behaviorEntry.getId());
            save(apLikesBehavior);
        }
        //或者取消点赞
        else {
            apLikesBehavior.setOperation(dto.getOperation());
            updateById(apLikesBehavior);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}